﻿Imports System.Web.UI.WebControls

Public Class ucEditApplication3
	Inherits System.Web.UI.UserControl

	Public Event NextStep()
	Public Event BackStep()
	Public Event Cancel()
	Public Event Alert(ByVal msg As String)

	Public Shared Function ConvertSide(ByVal side As String) As String
		If side.ToUpper.Equals("L") Then
			Return "ซ้าย"
		Else
			Return "ขวา"
		End If
	End Function

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			LoadHeader()
			If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
				LoadData()
			End If
		End If
		btnFalk.Attributes.Add("Style", "display:none")
	End Sub

	Private Sub LoadData()
		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.DataSourceEditOfficer
		With registerInfo

			Me.hfSponserID.Value = .officer_sponsor
			Me.OFFICER_SPONSOR.Text = .officer_sponsor_appid
			Me.OFFICER_SPONSOR_NAME.Text = .officer_sponsor_name
			Me.OFFICER_SPONSOR_SURENAME.Text = .officer_sponsor_surename
			Me.OFFICER_SPONSOR_TEL.Text = .officer_sponsor_tel

			Me.hfSenderID.Value = .officer_sender
			Me.OFFICER_SENDER.Text = .officer_sender_appid
			Me.OFFICER_SENDER_NAME.Text = .officer_sender_name
			Me.OFFICER_SENDER_SURENAME.Text = .officer_sender_surename
			Me.OFFICER_SENDER_TEL.Text = .officer_sender_tel

			Me.hfUplineID.Value = .officer_upline
			Me.OFFICER_UPLINE.Text = .officer_upline_appid
			Me.OFFICER_UPLINE_NAME.Text = .officer_upline_name
			Me.OFFICER_UPLINE_SURENAME.Text = .officer_upline_surename
			Me.OFFICER_UPLINE_TEL.Text = .officer_upline_tel
			Me.OFFICER_UPLINE_EMPTYSIDE.Text = .officer_upline_emptyside

			If Not String.IsNullOrEmpty(OFFICER_UPLINE_EMPTYSIDE.Text) Then
				If OFFICER_UPLINE_EMPTYSIDE.Text.Equals("L") Then
					OFFICER_UPLINE_EMPTYSIDE.Text = "ซ้าย"
				ElseIf OFFICER_UPLINE_EMPTYSIDE.Text.Equals("R") Then
					OFFICER_UPLINE_EMPTYSIDE.Text = "ขวา"
				End If
			End If

		End With
	End Sub

	Public Sub LoadHeader()
		Me.ucHeaderInfo1.LoadDataEdit()
	End Sub

	Protected Sub btnSponsorSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSponsorSearch.Click
		If HttpContext.Current.User.IsInRole("admin") Then
			Me.gvOffical.DataSource = BL.Officer.SearchSponser_Staff(Me.txtSearchSponsor.Text)
		Else
			Me.gvOffical.DataSource = BL.Officer.SearchSponser_Member(Me.txtSearchSponsor.Text, GlobalApp.Authen.offcier_id)
		End If
		Me.gvOffical.PageIndex = 0
		Me.gvOffical.DataBind()
		Me.mpu.Show()
		Me.Session("sponsorsearch") = Me.gvOffical.DataSource
	End Sub

	Protected Sub gvOffical_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOffical.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOffical.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			OFFICER_SPONSOR.Text = oj.Text
			OFFICER_SPONSOR_NAME.Text = gvRow.Cells(1).Text
			OFFICER_SPONSOR_SURENAME.Text = gvRow.Cells(2).Text
			OFFICER_SPONSOR_TEL.Text = gvRow.Cells(3).Text

			Dim hf As HiddenField
			hf = gvRow.Cells(4).FindControl("HiddenField1")
			Me.hfSponserID.Value = hf.Value

			Me.mpu.Hide()
		End If
	End Sub

	Protected Sub btnUplineSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUplineSearch.Click
		Me.gvOfficalUpline.DataSource = BL.Officer.SearchUpline(Me.txtSearchUpline.Text, Me.hfSponserID.Value)
		Me.gvOfficalUpline.PageIndex = 0
		Me.gvOfficalUpline.DataBind()
		Me.mpuUpline.Show()
		Me.Session("uplinesearch") = Me.gvOfficalUpline.DataSource
	End Sub

	Protected Sub gvOfficalUpline_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOfficalUpline.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOfficalUpline.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			OFFICER_UPLINE.Text = oj.Text
			OFFICER_UPLINE_NAME.Text = gvRow.Cells(1).Text
			OFFICER_UPLINE_SURENAME.Text = gvRow.Cells(2).Text
			OFFICER_UPLINE_TEL.Text = gvRow.Cells(3).Text
			Try
				OFFICER_UPLINE_EMPTYSIDE.Text = DirectCast(gvRow.Cells(4).FindControl("Label1"), Label).Text
			Catch ex As Exception
				Common.LogError.AddLogException(ex)
			End Try
			Dim hf As HiddenField
			hf = gvRow.Cells(5).FindControl("HiddenField1")
			Me.hfUplineID.Value = hf.Value
			Me.mpuUpline.Hide()
		End If
	End Sub

	Protected Sub btnSenderSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSenderSearch.Click
		Me.gvOfficalSender.DataSource = BL.Officer.SearchSender(Me.txtSearchSender.Text, Me.hfSponserID.Value)
		Me.gvOfficalSender.PageIndex = 0
		Me.gvOfficalSender.DataBind()
		Me.mpuSender.Show()
		Me.Session("sendersearch") = Me.gvOfficalSender.DataSource
	End Sub

	Protected Sub gvOfficalSender_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOfficalSender.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOfficalSender.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			OFFICER_SENDER.Text = oj.Text
			OFFICER_SENDER_NAME.Text = gvRow.Cells(1).Text
			OFFICER_SENDER_SURENAME.Text = gvRow.Cells(2).Text
			OFFICER_SENDER_TEL.Text = gvRow.Cells(3).Text
			Dim hf As HiddenField
			hf = gvRow.Cells(4).FindControl("HiddenField1")
			Me.hfSenderID.Value = hf.Value
			Me.mpuSender.Hide()
		End If
	End Sub

	Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
		'################################################
		'Validation
		'################################################
		If Not CheckSponsor() Then
			Exit Sub
		End If

		If Not CheckUpline() Then
			Exit Sub
		End If

		If Not CheckSender() Then
			Exit Sub
		End If
		'################################################

		Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
			registerInfo = GlobalApp.DataSourceEditOfficer
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo

			If Not String.IsNullOrEmpty(Me.OFFICER_SPONSOR.Text) Then
				.officer_sponsor = Me.hfSponserID.Value
				.officer_sponsor_appid = Me.OFFICER_SPONSOR.Text
				.officer_sponsor_name = Me.OFFICER_SPONSOR_NAME.Text
				.officer_sponsor_surename = Me.OFFICER_SPONSOR_SURENAME.Text
				.officer_sponsor_tel = Me.OFFICER_SPONSOR_TEL.Text
			End If

			If Not String.IsNullOrEmpty(Me.OFFICER_UPLINE.Text) Then
				.officer_upline = Me.hfUplineID.Value
				.officer_upline_appid = Me.OFFICER_UPLINE.Text
				.officer_upline_name = Me.OFFICER_UPLINE_NAME.Text
				.officer_upline_surename = Me.OFFICER_UPLINE_SURENAME.Text
				.officer_upline_tel = Me.OFFICER_UPLINE_TEL.Text
				If OFFICER_UPLINE_EMPTYSIDE.Text.Equals("ซ้าย") Then
					.officer_upline_emptyside = "L"
					.officer_upline_side = "L"
				Else
					.officer_upline_emptyside = "R"
					.officer_upline_side = "R"
				End If
			End If

			If Not String.IsNullOrEmpty(Me.OFFICER_SENDER.Text) Then
				.officer_sender = Me.hfSenderID.Value
				.officer_sender_appid = Me.OFFICER_SENDER.Text
				.officer_sender_name = Me.OFFICER_SENDER_NAME.Text
				.officer_sender_surename = Me.OFFICER_SENDER_SURENAME.Text
				.officer_sender_tel = Me.OFFICER_SENDER_TEL.Text
			End If

			.officer_downline_left = String.Empty
			.officer_downline_right = String.Empty

		End With

		GlobalApp.DataSourceEditOfficer = registerInfo
		RaiseEvent NextStep()

		RemoveSession()

		Response.Redirect("EditApplication3.aspx", False)
	End Sub

	Public Sub RemoveSession()
		Me.Session.Remove("sponsorsearch")
		Me.Session.Remove("uplinesearch")
		Me.Session.Remove("sendersearch")
	End Sub

	Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
		RemoveSession()
		Response.Redirect("EditApplication1.aspx", False)
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		RemoveSession()
		If HttpContext.Current.User.IsInRole("admin") Then
			Response.Redirect("OfficerManagement.aspx")
		Else
			Response.Redirect("ViewStatusRegister.aspx")
		End If
	End Sub

	Protected Sub gvOffical_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOffical.PageIndexChanging
		Me.gvOffical.DataSource = Me.Session("sponsorsearch")
		Me.gvOffical.PageIndex = e.NewPageIndex
		Me.gvOffical.DataBind()
		Me.mpu.Show()
	End Sub

	Protected Sub gvOfficalUpline_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOfficalUpline.PageIndexChanging
		Me.gvOfficalUpline.DataSource = Me.Session("uplinesearch")
		Me.gvOfficalUpline.PageIndex = e.NewPageIndex
		Me.gvOfficalUpline.DataBind()
		Me.mpuUpline.Show()
	End Sub

	Protected Sub gvOfficalSender_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOfficalSender.PageIndexChanging
		Me.gvOfficalSender.DataSource = Me.Session("sendersearch")
		Me.gvOfficalSender.PageIndex = e.NewPageIndex
		Me.gvOfficalSender.DataBind()
		Me.mpuSender.Show()
	End Sub

	Protected Sub imgUPLINEPopup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgUPLINEPopup.Click
		Me.mpuUpline.Show()
		btnUplineSearch_Click(Nothing, Nothing)
	End Sub

	Protected Sub imgSENDERPopup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgSENDERPopup.Click
		mpuSender.Show()
		btnSenderSearch_Click(Nothing, Nothing)
	End Sub

	Protected Sub imgSponsorPopup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgSponsorPopup.Click
		mpu.Show()
		btnSponsorSearch_Click(Nothing, Nothing)
	End Sub

	Private Function CheckSponsor() As Boolean
		If Not String.IsNullOrEmpty(Me.OFFICER_SPONSOR.Text) Then
			Try
				Dim officer As Entity.OfficerInfoItem
				If HttpContext.Current.User.IsInRole("admin") Then
					officer = BL.Officer.LoadSponser_Staff(Me.OFFICER_SPONSOR.Text)
				Else
					officer = BL.Officer.LoadSponser_Member(Me.OFFICER_SPONSOR.Text, GlobalApp.Authen.offcier_id)
				End If

				If officer IsNot Nothing Then
					OFFICER_SPONSOR.Text = officer.officer_app_id
					OFFICER_SPONSOR_NAME.Text = officer.officer_firstname_tha
					OFFICER_SPONSOR_SURENAME.Text = officer.officer_lastname_tha
					OFFICER_SPONSOR_TEL.Text = officer.offcier_app_tel
					Me.hfSponserID.Value = officer.officer_id
					Return True
				Else
					OFFICER_SPONSOR_NAME.Text = String.Empty
					OFFICER_SPONSOR_SURENAME.Text = String.Empty
					OFFICER_SPONSOR_TEL.Text = String.Empty
					Me.hfSponserID.Value = String.Empty
					RaiseEvent Alert("ไม่พบข้อมูลสปอนเซอร์")
					Return False
				End If
			Catch ex As Exception
				Common.LogError.AddLogException(ex)
				RaiseEvent Alert("พบข้อผิดพลาดในการค้นหาข้อมูลสปอนเซอร์")
				Return False
			End Try
		Else
			Return True
		End If
	End Function

	Private Function CheckUpline() As Boolean
		If String.IsNullOrEmpty(Me.OFFICER_UPLINE.Text) Then
			RaiseEvent Alert("กรุณาระบุข้อมูลอัพไลน์")
			Return False
		Else
			Return True
		End If
	End Function

	Private Function CheckSender() As Boolean
		If Not String.IsNullOrEmpty(Me.OFFICER_SENDER.Text) Then
			Try
				Dim officer As Entity.OfficerInfoItem = BL.Officer.LoadSender_Member(Me.OFFICER_SENDER.Text, Me.hfSponserID.Value)
				If officer IsNot Nothing Then
					OFFICER_SENDER.Text = officer.officer_app_id
					OFFICER_SENDER_NAME.Text = officer.officer_firstname_tha
					OFFICER_SENDER_SURENAME.Text = officer.officer_lastname_tha
					OFFICER_SENDER_TEL.Text = officer.offcier_app_tel
					Me.hfSenderID.Value = officer.officer_id
					Return True
				Else
					OFFICER_SENDER_NAME.Text = String.Empty
					OFFICER_SENDER_SURENAME.Text = String.Empty
					OFFICER_SENDER_TEL.Text = String.Empty
					Me.hfSenderID.Value = String.Empty
					RaiseEvent Alert("ไม่พบข้อมูลผู้ยื่นใบสมัคร")
					Return False
				End If
			Catch ex As Exception
				Common.LogError.AddLogException(ex)
				RaiseEvent Alert("พบข้อผิดพลาดในการค้นหาข้อมูลผู้ยื่นใบสมัคร")
				Return False
			End Try
		Else
			Return True
		End If
	End Function

	Protected Sub btnCheckSponsor_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheckSponsor.Click
		CheckSponsor()
	End Sub


	Protected Sub btnCheckSender_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheckSender.Click
		CheckSender()
	End Sub

End Class